package com.tiancheng.trade.merchant.controller.admin;

import com.tiancheng.trade.commom.core.data.PageInfo;
import com.tiancheng.trade.commom.core.data.Result;
import com.tiancheng.trade.merchant.enums.RevenueTypeEnum;
import com.tiancheng.trade.merchant.exception.BusinessException;
import com.tiancheng.trade.merchant.model.Merchant;
import com.tiancheng.trade.merchant.model.MerchantPaymentChannel;
import com.tiancheng.trade.merchant.service.IMerchantPaymentChannelService;
import com.tiancheng.trade.merchant.service.IMerchantService;
import com.tiancheng.trade.merchant.util.ResultJudgeUtil;
import com.tiancheng.trade.merchant.vo.merchant.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
//@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/admin/merchant")
public class MerchantAdminController {
    @Autowired
    IMerchantService merchantService;
    @Autowired
    IMerchantPaymentChannelService channelService;

    /**
     * 按id查询商户信息
     *
     * @param id {@link Long}
     * @return {@link Result}<{@link Merchant}>
     */
    @GetMapping("/{id}")
    public Result<Merchant> findById(@PathVariable Long id) throws BusinessException {
        Assert.isTrue(id != null, "参数错误~");
        return Result.success(merchantService.findById(id));
    }

    /**
     * 分页查询商户信息
     *
     * @param merchantVO {@link GetPageMerchantVO}
     * @return {@link Result}<{@link PageInfo}<{@link Merchant}>>
     */
    @GetMapping("/findPageList")
    public Result<PageInfo<Merchant>> findMerchantPage(GetPageMerchantVO merchantVO) throws BusinessException {
        return Result.success(merchantService.getMerchantPage(merchantVO));
    }

    /**
     * 查询商户信息列表
     *
     * @param merchantVO {@link GetMerchantVO}
     * @return {@link Result}<{@link List}<{@link Merchant}>>
     */
    @GetMapping("/findList")
    public Result<List<Merchant>> findList(GetMerchantVO merchantVO) throws BusinessException {
        return Result.success(merchantService.findList(merchantVO));
    }

    /**
     * 查询商户信息列表
     *
     * @param merchantVO {@link GetMerchantVO}
     * @return {@link Result}<{@link List}<{@link Merchant}>>
     */
    @GetMapping("/findOnlyMerchantList")
    public Result<List<AllMerchantResVo>> findOnlyMerchantList(GetMerchantVO merchantVO) throws BusinessException {
        return Result.success(merchantService.findOnlyMerchantList(merchantVO));
    }

    /**
     * 新增商户信息
     *
     * @param merchant {@link Merchant}
     */
    @PostMapping("/")
    public Result<?> add(@RequestBody Merchant merchant) throws BusinessException {
        return ResultJudgeUtil.judge(merchantService.add(merchant), "新增");
    }

    /**
     * 修改商户信息
     *
     * @param merchant {@link Merchant}
     */
    @PutMapping("/")
    public Result<?> update(@RequestBody Merchant merchant) throws BusinessException {
        Assert.isTrue(merchant.getId() != null, "参数错误~");
        return ResultJudgeUtil.judge(merchantService.update(merchant), "修改");
    }

    /**
     * 按商户id删除商户信息，同时删除商户渠道信息
     *
     * @param id {@link Long}
     */
    @DeleteMapping("/{id}")
    public Result<?> remove(@PathVariable Long id) throws BusinessException {
        Assert.isTrue(id != null, "参数错误~");
        return ResultJudgeUtil.judge(merchantService.remove(id), "删除");
    }

    /**
     * 按商户编码和支付渠道编码查询渠道信息 - 支付中心使用
     *
     * @param merchantCode       {@link String}
     * @param paymentChannelCode {@link String}
     * @return {@link Result}<{@link MerchantPaymentChannel}>
     */
    @GetMapping("/findSubAppid")
    public Result<MerchantPaymentChannel> findSubAppid(@RequestParam(name = "merchant_code") String merchantCode,
                                                       @RequestParam(name = "payment_channel_code") String paymentChannelCode)
            throws BusinessException {

        Assert.isTrue(merchantCode != null, "参数错误~");
        GetMerchantPaymentVO paymentVO = new GetMerchantPaymentVO();
        paymentVO.setMerchantCode(merchantCode);
        paymentVO.setPaymentChannelCode(paymentChannelCode);
        return Result.success(channelService.findSubAppid(paymentVO));
    }

    /**
     * 按商户编码和支付渠道编码批量查询渠道信息 - 支付中心使用
     *
     * @param list {@link List}<{@link GetMerchantPaymentVO}>
     * @return {@link Result}<{@link List}<{@link MerchantPaymentChannel}>>
     */
    @PostMapping("/findSubAppidList")
    public Result<List<MerchantPaymentChannel>> findSubAppidList(@RequestBody List<GetMerchantPaymentVO> list)
            throws BusinessException {

        Assert.isTrue(!(list == null || list.isEmpty()), "参数错误~");
        List<MerchantPaymentChannel> resultList = new ArrayList<>();
        for (GetMerchantPaymentVO merchantVO : list) {
            GetMerchantPaymentVO paymentVO = new GetMerchantPaymentVO();
            paymentVO.setMerchantCode(merchantVO.getMerchantCode());
            paymentVO.setPaymentChannelCode(merchantVO.getPaymentChannelCode());
            MerchantPaymentChannel pay = channelService.findSubAppid(paymentVO);
            resultList.add(pay);
        }
        return Result.success(resultList);
    }

    /**
     * 商户信息导出
     *
     * @param merchantVO {@link GetPageMerchantVO}
     */
    @PostMapping("/export")
    public void merchantExport(@RequestBody GetPageMerchantVO merchantVO, HttpServletRequest request,
                               HttpServletResponse response) throws IOException {
        merchantService.export(merchantVO, request, response);
    }

    /**
     * 商户-获取收入确认规则
     *
     * @return {@link Result}<{@link List}<{@link RevenueTypeVo}>>
     */
    @GetMapping("/allRevenueType")
    public Result<List<RevenueTypeVo>> getAllRevenueType() {

        List<RevenueTypeEnum> all = RevenueTypeEnum.getAll();
        List<RevenueTypeVo> revenueTypeVos = new ArrayList<>(all.size());
        for (RevenueTypeEnum e : all) {
            revenueTypeVos.add(new RevenueTypeVo(e.getRevenueType(), e.getRevenueTypeName(), e.getRevenueTypeDesc(), e.getCustom()));
        }
        return Result.success(revenueTypeVos);
    }

    /**
     * 商户-获取收入确认规则
     *
     * @param merchantCode {@link String}
     * @return {@link Result}<{@link List}<{@link MerchantPaymentChannel}>>
     */
    @GetMapping("/getMerchantPaymentChannelByMerchantCode")
    public Result<List<MerchantPaymentChannel>> getMerchantPaymentChannelByMerchantCode(@RequestParam("merchantCode") String merchantCode) {
        return Result.success(merchantService.getMerchantPaymentChannelByMerchantCode(merchantCode));
    }

}
